import java.util.HashSet;

/**
 * @author wangj881
 * @date 2024-12-05 18:47
 */
public class L202_2 {

    public static void main(String[] args) {
        boolean happy = new Solution().isHappy(19);
        System.out.println(happy);
    }

    static class Solution {
        public boolean isHappy(int n) {
            HashSet<Integer> set = new HashSet<>();
            set.add(n);
            return deal(set, n);
        }

        private boolean deal(HashSet<Integer> set, int n) {
            int sum = 0;
            while (true) {
                char[] chars = String.valueOf(n).toCharArray();
                for (char aChar : chars) {
                    int i = Integer.parseInt(String.valueOf(aChar));
                    sum += i * i;
                }
                System.out.println(sum);
                if (sum == 1) {
                    return true;
                }
                if (!set.add(sum)) {
                    return false;
                }
                n = sum;
                sum = 0;
            }
        }
    }
}
